package com.junjie.web.action.order;

import java.util.Date;
import java.util.List;

import com.junjie.dao.impl.OrderDaoImpl;
import com.junjie.dao.impl.ReceiveAddressDaoImpl;
import com.junjie.entity.Cart;
import com.junjie.entity.Order;
import com.junjie.entity.ReceiveAddress;
import com.junjie.entity.User;
import com.junjie.util.CookieUtil;
import com.junjie.web.action.BaseAction;

public class ProductAction extends BaseAction{
	private Cart cart = new Cart();
	private double needPay;
	private ReceiveAddress address;
	private ReceiveAddressDaoImpl addressDao = new ReceiveAddressDaoImpl();
	private String info;//填写地址时的提示信息
	private List<ReceiveAddress> addressList;//所有的地址集合
	private boolean unique;//ajax异步验证地址的唯一性
	private int id;//用于接收页面传过来的参数
	private Order order ;
	private OrderDaoImpl orderDao = new OrderDaoImpl();
	

	public String execute(){
		//1.检查用户是否已经登录,如果没有登录跳到登录页面,
		//1.看session中有没有存放用户信息:System.out.println(session.get("loginUser"));
		User user = (User)session.get("loginUser");
		if(user==null){
			return "needLogin";
		}else{
			try {
				String content = CookieUtil.findCookie("cart", request);
				cart.load(content);
				session.put("cart", cart);
				if(cart.getItems().size()==0){
					return "shopping";
				}
				needPay = cart.cost();
			} catch (Exception e) {
				e.printStackTrace();
				return "error";
			}
		}
		//读取cookie 得到用户购买的所有商品
		return "orderInfo";
	}
	/**
	 * 1.确认订单下一步
	 * @return
	 */
	public String nextOrder(){
		//读取session 获得用户信息
		User user = (User)session.get("loginUser");
		if(user==null){
			return "needLogin";
		}else{
			//用户已经登录,根据用户的ID 查询出数据库中的地址信息保存到集合中
		try {
		  addressList = addressDao.findByUserId(user);
		} catch (Exception e) {
			e.printStackTrace();
			return "error";
		}
		}
		return "addressForm";
	}
	/**
	 * 2.填写送货地址下一步
	 * 执行的操作:添加订单信息到数据库 ,跳转到订单成功页面
	 */
	public String writeAddress(){
		//1.获取session中的用户id
		User user = (User)session.get("loginUser");
		if(user == null){
			return "needLogin";
		}else{
			try {
			if(addressDao.findById(address.getId()) != null){
				//用户之前已经添加过此地址
			}else{
				//插入地址信息到d_receive_address表中
				address.setUserId(user.getId());
				addressDao.addReceviceAddress(address);
			}
			//插入订单信息到d_order表中//订单状态默认为:0未完成
			order = new Order();
			order.setUserId(user.getId());
			order.setStatus(0);
			order.setOrderTime(new Date().getTime());
			String totalPrice = (String)session.get("dangSum").toString();
			order.setTotalPrice(Double.parseDouble(totalPrice));
			order.setId( orderDao.addOrder(order));
			System.out.println(order.getId());
		} catch (Exception e) {
			e.printStackTrace();
			return "error";
		}
		CookieUtil.deleteCookie("cart", response);
		session.put("cart", null);
		return "orderOk";
		}
	}
	/**
	 * 根据用户选择是否使用旧地址还是重新填写新地址判断
	 */
	
	public String optionAdd(){
		try {
			address = addressDao.findById(id);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return "optionaddress";
		
		
	}
	public Cart getCart() {
		return cart;
	}
	public void setCart(Cart cart) {
		this.cart = cart;
	}
	public double getNeedPay() {
		return needPay;
	}
	public void setNeedPay(double needPay) {
		
		this.needPay = needPay;
	}

	public ReceiveAddress getAddress() {
		return address;
	}
	public void setAddress(ReceiveAddress address) {
		this.address = address;
	}
	public String getInfo() {
		return info;
	}
	public void setInfo(String info) {
		this.info = info;
	}
	public List<ReceiveAddress> getAddressList() {
		return addressList;
	}
	public void setAddressList(List<ReceiveAddress> addressList) {
		this.addressList = addressList;
	}

	public boolean isUnique() {
		return unique;
	}
	public void setUnique(boolean unique) {
		this.unique = unique;
	}
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public Order getOrder() {
		return order;
	}
	public void setOrder(Order order) {
		this.order = order;
	}
}
